import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response } from 'express';
import * as log4js from 'log4js'
import {Auth} from '../../auth/auth'
import {getLogger} from '../logs/logger_manager'

let logger = getLogger();

@Injectable()
export class GetToken implements NestMiddleware {
  async use(req: Request, res: Response, next: Function) {
    const method = req.method;
    const authorization = req.get('Authorization');
    if(authorization)
    {
        const auth = new Auth();
        let userInfo =  await auth.getUserInfo(authorization);
        if(userInfo == undefined)
            userInfo = {};
        req['userInfo'] = userInfo;
        if(method == "GET")
        {
            logger.info(JSON.stringify(req.query));
        }
        //post
        else
        {
            logger.info(JSON.stringify(req.body));
        }

    }
    next();
  }
}
